package com.file.controller;

import java.io.BufferedReader;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.entity.Terminal;
import com.file.entity.FileTerminal;
import com.file.service.TerminalService;
import com.setting.service.SysdicService;
import com.utils.JsonResult;
import com.utils.JsonSysdic;

/**
 * @Title: TerminalController.java
 * @Package com.controller
 * @author 孙梦楠
 * @date 2017年7月24日 下午2:12:08
 * @version V1.0.0
 */

@RestController
@RequestMapping(value = "/file/terminal")
public class TerminalController {

	@Autowired
	private TerminalService terminalservice;

	@Autowired
	private SysdicService sysdicservice;

	private static final Logger log = LoggerFactory.getLogger(TerminalController.class);

	// 条件查询列表
	@RequestMapping(value = "", method = RequestMethod.POST)
	public JsonResult<List<FileTerminal>> listTerminalByConditions(

			@RequestBody Terminal terminal,

			@CookieValue(value = "opeorgcookie", required = false) String opeorgcookie,
			@CookieValue(value = "opetypecookie", required = false) String opetypecookie,
			@CookieValue(value = "languagecookie", required = false) String languagecookie,
			HttpServletRequest request) {

		String terminalcode = terminal.getTerminalcode();
		String terminalassetcode = terminal.getTerminalassetcode();
		String terminaltype = terminal.getTerminaltype();
		String terminaladdress = terminal.getTerminaladdress();
		String terminalcommuncate = terminal.getTerminalcommuncate();
		String terminalmade = terminal.getTerminalmade();
		String terminalstatus = terminal.getTerminalstatus();

		return terminalservice.listTerminalByConditions(terminalcode, terminalassetcode, terminaltype, terminaladdress,
				terminalcommuncate, terminalmade, opeorgcookie, terminalstatus, opetypecookie, request, languagecookie);

	}

	// 字典列表
	@RequestMapping(value = "/listsysdicTerminaltype", method = RequestMethod.GET)
	public JsonResult<List<JsonSysdic>> listsysdicTerminaltype(
			@CookieValue(value = "opetypecookie", required = false) String opetypecookie,
			@CookieValue(value = "languagecookie", required = false) String languagecookie) {

		String sysdictype = sysdicservice.gettype(opetypecookie, "1");// 集中器 1
		Integer state = 226;
		String Message = "Terminaltype字段列表";

		return sysdicservice.listsysdic(sysdictype, state, Message, languagecookie);
	}

	@RequestMapping(value = "/listsysdicTerminalcommuncate", method = RequestMethod.GET)
	public JsonResult<List<JsonSysdic>> listsysdicTerminalcommuncate(
			@CookieValue(value = "languagecookie", required = false) String languagecookie) {

		String sysdictype = "0007";
		Integer state = 227;
		String Message = "Terminalcommuncate字段列表";

		return sysdicservice.listsysdic(sysdictype, state, Message, languagecookie);
	}

	@RequestMapping(value = "/listsysdicTerminalmade", method = RequestMethod.GET)
	public JsonResult<List<JsonSysdic>> listsysdicTerminalmade(
			@CookieValue(value = "languagecookie", required = false) String languagecookie) {

		String sysdictype = "0008";
		Integer state = 228;
		String Message = "Terminalmade字段列表";

		return sysdicservice.listsysdic(sysdictype, state, Message, languagecookie);
	}

	@RequestMapping(value = "/listsysdicTerminalstatus", method = RequestMethod.GET)
	public JsonResult<List<JsonSysdic>> listsysdicTerminalstatus(
			@CookieValue(value = "languagecookie", required = false) String languagecookie) {

		String sysdictype = "0009";
		Integer state = 229;
		String Message = "Terminalstatus字段列表";

		return sysdicservice.listsysdic(sysdictype, state, Message, languagecookie);
	}

	// 增加
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public JsonResult<Terminal> addterminal(@RequestBody Terminal terminal,
			@CookieValue(value = "opeorgcookie", required = false) String opeorgcookie,
			@CookieValue(value = "opetypecookie", required = false) String opetypecookie) {

		String terminalcode = terminal.getTerminalcode();
		String terminalassetcode = terminal.getTerminalassetcode();
		String terminaltype = terminal.getTerminaltype();
		String terminaladdress = terminal.getTerminaladdress();
		String terminalphysicaladdress = terminal.getTerminalphysicaladdress();
		String terminalcommuncate = terminal.getTerminalcommuncate();
		String terminalmade = terminal.getTerminalmade();

		return terminalservice.addTerminal(terminalcode, terminalassetcode, terminaltype, terminaladdress,
				terminalcommuncate, terminalmade, opeorgcookie, opetypecookie, terminalphysicaladdress);

	}

	// 删除
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public JsonResult<Terminal> deleteterminal(@RequestBody Terminal terminal) {

		Integer terminalid = terminal.getTerminalid();
		return terminalservice.deleteterminal(terminalid);

	}

	// 强制删除
	@RequestMapping(value = "/force_delete", method = RequestMethod.POST)
	public JsonResult<Terminal> foece_deleteterminal(@RequestBody Terminal terminal) {

		Integer terminalid = terminal.getTerminalid();
		return terminalservice.forcedeleteterminal(terminalid);

	}

	// 修改页面
	@RequestMapping(value = "/getbyid", method = RequestMethod.POST)
	public JsonResult<FileTerminal> updateterminal(@RequestBody Terminal terminal,
			@CookieValue(value = "languagecookie", required = false) String languagecookie) {
		Integer terminalid = terminal.getTerminalid();
		return terminalservice.updateviewterminal(terminalid, languagecookie);

	}

	// 修改
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public JsonResult<Terminal> updateterminal(

			@RequestBody Terminal terminal, @CookieValue(value = "opeorgcookie", required = false) String opeorgcookie,
			@CookieValue(value = "opetypecookie", required = false) String opetypecookie) {

		// log.info("terminalcode " + terminal.getTerminalcode());
		// log.info("terminalassetcode " + terminal.getTerminalassetcode());
		// log.info("terminaltype " + terminal.getTerminaltype());
		// log.info("terminaladdress " + terminal.getTerminaladdress());
		// log.info("terminalcommuncate " + terminal.getTerminalcommuncate());
		// log.info("opeorgcookie " + opeorgcookie);
		// log.info("terminalmade " + terminal.getTerminalmade());
		// log.info("terminalstatus " + terminal.getTerminalstatus());

		Integer terminalid = terminal.getTerminalid();
		String terminalcode = terminal.getTerminalcode();
		String terminalassetcode = terminal.getTerminalassetcode();
		String terminaltype = terminal.getTerminaltype();
		String terminaladdress = terminal.getTerminaladdress();
		String terminalphysicaladdress = terminal.getTerminalphysicaladdress();
		String terminalcommuncate = terminal.getTerminalcommuncate();
		String terminalmade = terminal.getTerminalmade();
		String terminalstatus = null;
		return terminalservice.updateTerminal(terminalid, terminalcode, terminalassetcode, terminaltype,
				terminaladdress, terminalcommuncate, terminalmade, opeorgcookie, terminalstatus, opetypecookie,
				terminalphysicaladdress);

	}

}
